export default {
  objectCopy: function (data) {
    return JSON.parse(JSON.stringify(data));
  },
  isObject: (params) =>
    Object.prototype.toString.call(params) === "[object Object]",
  isArray: (params) =>
    Object.prototype.toString.call(params) === "[object Array]",
  isFunction: (params) =>
    Object.prototype.toString.call(params) === "[object Function]",
  toSizeText: (size) => {
    let num = 0;
    let unit = "";
    if (size / 1024 < 1000) {
      num = size / 1024;
      unit = "KB";
    } else if (size / 1024 / 1024 < 1000) {
      num = size / 1024 / 1024;
      unit = "MB";
    } else {
      num = size / 1024 / 1024 / 1024;
      unit = "GB";
    }
    num = Math.floor(num * 100) / 100;
    return `${num}${unit}`;
  },
  // 获取localStorage的已存容量
  getLocalSurplus: () => {
    if (!window.localStorage) {
      console.log("浏览器不支持localStorage");
    }
    var size = 0;
    for (let item in window.localStorage) {
      if (window.localStorage.hasOwnProperty(item)) {
        size += window.localStorage.getItem(item).length;
      }
    }

    if (parseInt((size / 1024).toFixed(2)) > 4000) {
      window.localStorage.clear();
    }
    console.log("当前localStorage已存容量为" + (size / 1024).toFixed(2) + "KB");
  },
  parseFormatNum: (number) => {
    if (!number) return;
    const a = number.toString().split("");
    let v = a.reverse();
    let vn = "";
    for (let i = 0; i < v.length; i++) {
      vn += v[i] + ((i + 1) % 3 == 0 && i + 1 != v.length ? "," : "");
    }
    return vn.split("").reverse().join("");
  },
  getQueryString: (h, name) => {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    let r = h.substr(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null;
  },
  openNewWindow: (url) => {
    window.open(window.location.href.split("#")[0] + "#" + url);
  },
  // 获取地址栏搜索参数 search 格式，如： ?deal_status=2
  generateParams: (search) => {
    let result = {};
    let searchParamsStr = search.split("?")[1];
    if (searchParamsStr) {
      let searchParams = searchParamsStr.split("&");
      searchParams.forEach((elem) => {
        let arr = elem.split("=");
        let key = arr[0];
        let value = arr[1];
        if (key) {
          result[key] = decodeURIComponent(value);
        }
      });
    }
    return result;
  },
  // 生成一个唯一随机数
  createRandom: () => {
    return (
      new Date().getTime() +
      "_" +
      parseInt(Math.random() * 10000) +
      "_" +
      parseInt(Math.random() * 10000)
    );
  },
  // 输入框输入整数处理
  integerHandle: (value) => {
    if (isNaN(parseInt(value))) {
      return "";
    } else {
      return parseInt(value) + "";
    }
  },

  idCardTest: (value) => {
    let re = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    return re.test(value);
  },
  phoneTest: (value) => {
    let re = /^(1(3[0-9]|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-9]))\d{8}$/;
    return re.test(value);
  },

  // 密码格式策略   rules_type: 规则类型
  pwdRegStrategy: (value, rules_type) => {
    let regExp0 =
      /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、]).+$/;
    let regExp1 = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[\s\S]+$/;
    let regExp2 = /^(?=.*?[A-Z])(?=.*?[a-z]).+$/;
    let regExp3 = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/;

    // 0-大小写字母加数字加特殊字符 1-大小写字母加数字 2-大小写字母
    switch (rules_type) {
      case "0":
        return regExp0.test(value);
      case "1":
        return regExp1.test(value);
      case "2":
        return regExp2.test(value);
      case "3":
        return regExp3.test(value);
      default:
        return false;
    }
  },
  // 密码字符串，保留大小写字母、数字、特殊字符
  pwdString: (str) =>
    str.replace(
      /([^A-Za-z0-9`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、])/g,
      ""
    ),
  // 密码强度策略   type: 0-大小写字母+数字+特殊字符, 1-大小写字母+数字, 2-大小写字母, 3-字母+数字且不少于6位
  pwdTest: (value, type) => {
    let regExp0 =
      /^(((?=.*[0-9])(?=.*[a-zA-Z])(?=.*[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、]))[^\s]+)$/g;
    let regExp1 = /^(((?=.*[0-9])(?=.*[a-zA-Z]))[^\s]+)$/g;
    let regExp2 = /^((?=.*[a-zA-Z])[^\s]+)$/g;
    let regExp3 = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,}$/g;

    switch (type) {
      case 0:
        return regExp0.test(value);
      case 1:
        return regExp1.test(value);
      case 2:
        return regExp2.test(value);
      case 3:
        return regExp3.test(value);
      default:
        return false;
    }
  },
};
